import {hrAjax as proFetch} from 'src/hrpub/common/utils/utils';
import handleAllpks from "./handleAllpks";
import {agreeFormTypeMap, formToServer, custFormToServer} from "./configData";
import {setFormAttrAndValue} from "../../../../../public/functions/checkFormAction";

export default function viewAgree(record) {
    let typeConf = agreeFormTypeMap[record.conttype.value][record.agreementtype.value];
    const {json, orgVal} = this.state;
    let mainArea, areas, postData;
    if (typeConf) {
        mainArea = typeConf.mainArea;
        areas = typeConf.areas;
        postData = formToServer[mainArea];
    } else {
        /**
         * 和后台约定：
         * 自定义类型表单区域名称为协议类型+Card
         * 如果是协议变更，则需要有变更前变更后，命名规则为协议类型+Card+Before和After
         * 如果出错，找后端确认命名是否规范
         */
        mainArea = record.agreementtype.value;
        areas = mainArea + 'Card';
        if (record.conttype.value === '3') {
            areas = ['agreementCard', areas + 'Before', areas + 'After'];
        } else {
            areas = [areas];
        }
        postData = {
            ...custFormToServer[record.conttype.value],
            areaCode: !typeConf && record.conttype.value === '3' ? areas.join(','):  areas[0]
        };
    }
    const agreeAppCode = postData.appCode;
    postData = {
        ...postData,
        pk_org: orgVal.refpk,
        pk_psndoc_sub: record.pk_agreement.value,
        tempFlag: true,
        isContract: false
    };
    proFetch({
        url: '/nccloud/hrcm/common/QueryDataCardAction.do',
        body: postData,
    })
        .then((res) => {
            if (res.success) {
                this.setState({
                        agreeArea: ''
                    }, () => {
                        const allpks = this.props.table.getAllTableData('agreementList').rows.map(item => item.values.pk_agreement.value);
                        handleAllpks.set(allpks);
                        let template = this.props.meta.getMeta();
                        let isRadio = false;
                        areas.forEach(item => {
                            /*if (item.indexOf('CardBefore') > -1) {
                                res.data.template[item].items.forEach(item2 => {
                                    item2.attrcode = item2.attrcode + '_old';
                                    item2.disabled = true;
                                })
                            }*/
                            const ifpropItem = res.data.template[item].items.find(item2 => item2.attrcode === 'ifprop');
                            isRadio = ifpropItem && ifpropItem.itemtype === 'radio';
                            template[item] = res.data.template[item];
                        });
                        const [area, ...subArea] = areas;
                        let editArea = area;
                        if (subArea.length) {
                            template['formrelation'] = {
                                ...(template['formrelation'] || {}),
                                [area]: subArea
                            };
                            editArea = subArea[subArea.length - 1];
                        }
                        let registed = this.registed;
                        for (let key in template) {
                            if(template.hasOwnProperty(key) && template[key].moduletype){
                                if(!registed.includes(template[key].code)) {
                                    template[key].moduletype === 'form' && this.props.use.form(template[key].code);
                                    template[key].moduletype === 'table' && this.props.use.editTable(template[key].code);
                                }
                        　　}
                        }
                        this.props.meta.setMeta(template,()=>{
                            if (res.data.attrMap) {
                                setFormAttrAndValue(res.data.attrMap, res.data.valueMap, this.props, area, areas)
                            }
                        });
                        const areaValue = res.data.data[area];
                        if (isRadio) {
                            if (areaValue.rows[0].values.hasOwnProperty('ifprop')) {
                                let ifprop = areaValue.rows[0].values.ifprop.value;
                                areaValue.rows[0].values.ifprop.value = ifprop ? 'Y' : 'N';
                                areaValue.rows[0].values.ifprop.display = ifprop ? json['cm6011-000057'] : json['cm6011-000058'];
                            }
                            if (areaValue.rows[0].values.hasOwnProperty('ifprop_old')) {
                                let ifprop_old = areaValue.rows[0].values.ifprop_old.value;
                                areaValue.rows[0].values.ifprop_old.value = ifprop_old ? 'Y' : 'N';
                                areaValue.rows[0].values.ifprop_old.display = ifprop_old ? json['cm6011-000057'] : json['cm6011-000058'];
                            }
                        }
                        this.setState({
                            agreeArea: area,
                            agreeAllpks: allpks,
                            agreeData: areaValue,
                            agreeAppCode,
                            agreementPage: 'view',
                            agreeSubArea: subArea,
                            agreePageid: res.data.template.pageid
                        }, () => {
                            this.props.form.setAllFormValue({[area]: areaValue});
                            /*if (isRadio && areaValue.rows[0].values.hasOwnProperty('ifprop')) {
                                let ifprop = areaValue.rows[0].values.ifprop.value;
                                let ifprop_old = areaValue.rows[0].values.ifprop_old && areaValue.rows[0].values.ifprop_old.value;
                                this.props.form.setFormItemsValue([area], {
                                    ifprop: {value: ifprop ? 'Y' : 'N'}, ifprop_old: {value: ifprop_old ? 'Y' : 'N'}
                                });
                            }*/
                            if (subArea && subArea.length) {
                                subArea.forEach(item => this.props.form.openArea(item))
                            }
                            this.updateButtonStatus();
                            if (areaValue.rows[0].values.lastflag && areaValue.rows[0].values.lastflag.value) {
                                this.props.button.setButtonDisabled({
                                    edit: false
                                });
                            } else {
                                this.props.button.setButtonDisabled({
                                    edit: true
                                });
                            }
                        })
                    }
                )
            }
        });
}
